package Cashier;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Cashier {// Leong Vai Cheong

	private static int No = 10000;
	private static Item[] itemList;
	
	List<Item> items = null;
	
	private Item[] orderList;
	
	private int orderCount;
	private int	orderNo;
	private int orderSize = 10;	
	private double total;
	private double received;
	private double changes;
	private String operator;
	private String date;	
	
	public Cashier() {
		this.orderList = new Item[orderSize];
		this.orderNo = ++No;
	}

	public Cashier(Item[] itemList) {
		Cashier.itemList = itemList;
		this.orderList = new Item[orderSize];
		this.orderNo = ++No;
	}

	public void addItemToOrderList(String barCodes) {
		boolean same = false;
		int i = 0, j = 0;

		for (j = 0; j < this.orderList.length; j++) {
			if (this.orderList[j] == null)	break;

			else if (this.orderList[j].getBarcode().equals(barCodes)) {
				this.orderList[j].setPurchaseQty(this.orderList[j].getPurchaseQty() + 1);
				same = true;
				break;
			}
		}

		if (j >= this.orderList.length) {
			System.out.println("Order Full! " + "Order maximum size: " + orderSize);
			return;
		}

		for (i = 0; i < Cashier.itemList.length; i++) {
			if (Cashier.itemList[i].getBarcode().equals(barCodes)) {
				if (!same) {
					this.orderList[j] = new Item();
					this.orderList[j].setBarcode(Cashier.itemList[i].getBarcode());
					this.orderList[j].setDescription(Cashier.itemList[i].getDescription());
					this.orderList[j].setPrice(Cashier.itemList[i].getPrice());
					this.orderList[j].setPurchaseQty(1);
					this.orderCount++;
				}
				Cashier.itemList[i].setPurchaseQty(Cashier.itemList[i].getPurchaseQty() - 1);
			}
		}
		this.setDate();
	}

	public void printReceipt() {
		System.out.println("OrderNo: "+this.orderNo+"\t\t\tDate:");
		System.out.println("Operator: "+this.operator+"\t\t\t"+this.getDate());
			
		this.total = Cashier.printItemsInfo(this.orderList, this.orderCount);
	
		System.out.println("\t\t\t Total Amount:   "+this.total);
		
		System.out.print("\t\t\t Input Payment:  ");
		BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
		try {
				String received = d.readLine();
				this.received = Double.parseDouble(received);
				
		} catch (IOException e) {
			e.printStackTrace();
		}			
		this.changes = this.received - this.total;
		System.out.println("\t\t\t Change Rate:    "+this.changes);
	}

	public static double printItemsInfo(Item[] a, int length) {		
		double total = 0;
		System.out.println("---------------------------------------------------");
		System.out.println("Barcode:\tDescription:\tQuant:\t UnitPrice:");			
		for (int i = 0; i < length; i++) {
			System.out.println(a[i].getBarcode() + "\t" + a[i].getDescription()
					+ "\t  " + a[i].getPurchaseQty()  + "\t " + a[i].getPrice());
			total += a[i].getPrice() * a[i].getPurchaseQty();
		}		
		System.out.println("---------------------------------------------------");
		return total;
	}

	public int getOrderSize() {
		return this.orderSize;
	}

	public void setOrderSize(int orderSize) {
		this.orderSize = orderSize;
		this.orderList = new Item[this.orderSize];
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public void setDate() {
		Date date = new Date(System.currentTimeMillis());
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
		String dt = df.format(date);
		this.date = dt;
	}
	
	public static int getNo() {
		return No;
	}

	public static void setNo(int no) {
		No = no;
	}

	public static Item[] getItemList() {
		return itemList;
	}

	public static void setItemList(Item[] itemList) {
		Cashier.itemList = itemList;
	}

	public Item[] getOrderList() {
		return orderList;
	}

	public void setOrderList(Item[] orderList) {
		this.orderList = orderList;
	}

	public int getOrderCount() {
		return orderCount;
	}

	public void setOrderCount(int orderCount) {
		this.orderCount = orderCount;
	}

	public int getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(int orderNo) {
		this.orderNo = orderNo;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public double getReceived() {
		return received;
	}

	public void setReceived(double received) {
		this.received = received;
	}

	public double getChanges() {
		return changes;
	}

	public void setChanges(double changes) {
		this.changes = changes;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

}